package com.xyy.saas.payment.converter;

import com.xyy.saas.payment.adpater.jdCredit.req.JDRefundNotifyReq;
import com.xyy.saas.payment.adpater.jdCredit.req.LoanSceneData;
import com.xyy.saas.payment.adpater.jdCredit.req.PushLoanSceneDataRequest;
import com.xyy.saas.payment.adpater.jdCredit.resp.OrdersInfo;
import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.cores.param.JDPushLoanSceneDto;
import com.xyy.saas.payment.dao.model.RefundOrder;
import com.xyy.saas.payment.payment.core.bo.RefundBO;
import com.xyy.saas.payment.util.LeafUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author liujf
 */
public class RefundConverter {

    public static RefundBO reqToSuccessBo(JDRefundNotifyReq request) {
        RefundBO refundBO = new RefundBO();
        refundBO.setDate(new Date());
        refundBO.setRefundNo(request.getBizNo());
        refundBO.setStatus(RefundOrder.STATUS_FINISH);
        return refundBO;
    }

    public static RefundBO reqToFailBo(JDRefundNotifyReq request) {
        RefundBO refundBO = new RefundBO();
//        refundBO.setDate(new Date());
        refundBO.setRefundNo(request.getBizNo());
        refundBO.setStatus(RefundOrder.STATUS_FAIL);
        return refundBO;
    }

    public static PushLoanSceneDataRequest sceneDtoToReq(JDPushLoanSceneDto dto, String reqNo) {
        PushLoanSceneDataRequest request = new PushLoanSceneDataRequest();
        request.setReqNo(reqNo);
        request.setReqDate(DateUtils.formatDate(new Date(), Constants.YYYYMMDDHHMMSS));
        request.setProductCode(Constants.JD_CREDIT_PRODUCT_CODE);
        request.setPartnerCode(Constants.JD_CREDIT_PARTNER_CODE);
        request.setCoreCompanyCode(Constants.JD_CREDIT_COMPANY_CODE);
        List<JDPushLoanSceneDto.SceneData> sceneDataList = dto.getSceneDataList();
        if (CollectionUtils.isNotEmpty(sceneDataList)) {
            List<LoanSceneData> loanSceneDataList = new ArrayList<>(sceneDataList.size());
            for (JDPushLoanSceneDto.SceneData sceneData : sceneDataList) {
                LoanSceneData loanSceneData = new LoanSceneData();
                loanSceneData.setCustCode(sceneData.getCustCode());
                loanSceneData.setFirstOrderCreateTime(sceneData.getFirstOrderCreateTime());
                loanSceneData.setLastOrderCreateTime(sceneData.getLastOrderCreateTime());
                loanSceneData.setOrderList(convertOrderList(sceneData.getOrderList()));
                loanSceneDataList.add(loanSceneData);
            }
            request.setSceneDataList(loanSceneDataList);
        }
        return request;
    }

    private static List<OrdersInfo> convertOrderList(List<JDPushLoanSceneDto.OrderInfo> orderList) {
        if (CollectionUtils.isEmpty(orderList)){
            return Collections.emptyList();
        }

        List<OrdersInfo> infoList = new ArrayList<>(orderList.size());
        for (JDPushLoanSceneDto.OrderInfo orderInfo : orderList) {
            OrdersInfo info = new OrdersInfo();
            info.setOrderCreateMth(orderInfo.getOrderCreateMth());
            info.setOrderAmount(orderInfo.getOrderAmount());
            info.setRefundAmount(orderInfo.getRefundAmount());
            info.setOrderCnt(orderInfo.getOrderCnt());
            info.setRefundCnt(orderInfo.getRefundCnt());
            info.setPaidAmount(orderInfo.getPaidAmount());
            info.setPaidCnt(orderInfo.getPaidCnt());
            info.setInvoiceAmount(orderInfo.getInvoiceAmount());
            infoList.add(info);
        }
        return infoList;
    }
}
